package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-24
 * Time: 18:34
 */
public enum TestEnum {
    RED(1, "红色"),GREEN(2, "绿色"),BLACK(3, "黑色");
    private int ordinal;
    private String color;
    //构造方法默认为私有的
    private TestEnum(int ordinal, String color) {
        this.ordinal = ordinal;
        this.color = color;
    }

    public static void main(String[] args) {
        //比较两个枚举成员定义时的顺序
        System.out.println(RED.compareTo(BLACK));
    }

    public static void main3(String[] args) {
        //以普通字符串转换为枚举实例
        TestEnum testEnum = TestEnum.valueOf("GREEN");
        System.out.println(testEnum);
    }
    public static void main2(String[] args) {
        TestEnum[] testEnum = TestEnum.values();
        for(int i = 0; i < testEnum.length; i++) {
            //打印枚举所有变量的序号
            System.out.println(testEnum[i].ordinal);
            //打印枚举所有变量的下标
            System.out.println(testEnum[i].ordinal());
            //打印枚举所有变量的color值
            System.out.println(testEnum[i].color);
        }
    }
    public static void main1(String[] args) {
        TestEnum color = RED;
        switch (color) {
            case RED:
                System.out.println("红色");
                break;
            case GREEN:
                System.out.println("绿色");
                break;
            case BLACK:
                System.out.println("黑色");
                break;
            default:
                System.out.println("error");
                break;
        }
    }
}
